package tt.dz.biz.service.smssend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import tt.dz.common.LogUtil;

public final class SendSMSHelpHandler
{
  public static String post(String url, Map<String, String> params, String encode)
    throws Exception
  {
    HttpClient httpclient = HttpClients.createDefault();
    String body = null;
    HttpPost httpost = new HttpPost(url);
    List<NameValuePair> nvps = new ArrayList();
    Set<String> keySet = params.keySet();
    for (String key : keySet) {
      nvps.add(new BasicNameValuePair(key, (String)params.get(key)));
    }
    try
    {
      httpost.setEntity(new UrlEncodedFormEntity(nvps, encode));
      HttpResponse response = httpclient.execute(httpost);
      HttpEntity entity = response.getEntity();
      body = EntityUtils.toString(entity);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      LogUtil.error("请求短信接口发生异常", e);
      throw e;
    }
    return body;
  }
}
